/*
 * @Author: 哈兰德
 * @Description: 移动端http请求封装
 */
import axios from 'axios'
import { LEGO_API_HOST } from '@/config/default'

// 请求超时时间
axios.defaults.timeout = 10000

// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
// axios.defaults.headers.post['Access-Control-Allow-Origin'] = '*'
// 
// axios.defaults.withCredentials=true

/** 
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get (url, params) {
  // url=LEGO_API_HOST+url
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    })
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}
/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function postapi (url, params) {
  url = LEGO_API_HOST + url
  return new Promise((resolve, reject) => {
    axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
    axios.post(url, JSON.stringify(params))
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}

